﻿using Microsoft.AspNetCore.Http;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using VIP.Infrastructure;
using VIP.Infrastructure.Exceptions;

namespace VIP.Application.Modules
{
    public class VIPHttpContext : IVIPHttpContext
    {
        private string session;

        private IHttpContextAccessor _httpContext;

        public const string HeaderToken = "Author";

        public VIPHttpContext(IHttpContextAccessor httpContext)
        {
            _httpContext = httpContext;
        }
        public string Session
        {
            get
            {
                if (String.IsNullOrEmpty(session))
                {
                    session = _httpContext.HttpContext.Request.Headers[HeaderToken].SingleOrDefault();
                }
                if (String.IsNullOrEmpty(session))
                {
                    throw new VIPAuthenticationException("session token lost");
                }
                return session;
            }
        }
    }
}
